package version4.Client.CircuitBreaker;

import java.util.HashMap;
import java.util.Map;

// 单例类,懒汉模式
public class CircuitBreakerProvider {
    private static CircuitBreakerProvider provider;
    private Map<String,CircuitBreaker> circuitBreakerMap;

    private CircuitBreakerProvider(){
        circuitBreakerMap = new HashMap<>();
    }
    public static CircuitBreakerProvider getInstance(){
        if(provider == null){
            provider = new CircuitBreakerProvider();
        }
        return provider;
    }
    public synchronized CircuitBreaker getCircuitBreaker(String serviceName){
        CircuitBreaker circuitBreaker;
        if(!circuitBreakerMap.containsKey(serviceName)){
            circuitBreaker = new CircuitBreaker(0.3,0.5,10000);
            circuitBreakerMap.put(serviceName, circuitBreaker);
        }
        return circuitBreakerMap.get(serviceName);
    }
}
